/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.MySQL;

import DAO.DAOInstanciaSubFase;
import InstanciaJogo.FasesJogo.InstanciaSubFasePattern;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;

/**
 * DAOInstanciaSubFaseInterfaceMySQL
 *
 * Versao 1.0
 *
 * Data Criação 13/02/2014
 *
 * @author Igor
 */
public class DAOInstanciaSubFaseMySQL extends DAOConexaoMySQL implements DAOInstanciaSubFase {
    @Override
    public InstanciaSubFasePattern buscaInstanciaSubFase(long idJogo) throws Exception {
        return null;
    }

    @Override
    public long gravaInstanciaSubFase(long idJogo, long idFase, long idSubFase) throws Exception {
        long vReturn = -1;

        CallableStatement cs;
        cs = this.con.getCon().prepareCall("{ ? = call GRAVA_INSTANCIA_SUB_FASE(?,?,?)}");

        cs.registerOutParameter(1, Types.NUMERIC);
        cs.setLong(2, idJogo);
        cs.setLong(3, idFase);
        cs.setLong(4, idSubFase);
        cs.execute();

        vReturn = cs.getLong(1);
        this.con.getStm().executeBatch();

        return vReturn;
    }

    @Override
    public long buscaPorcentagemAcerto(long idInstanciaSubFase) throws Exception {
        long porcentagemAcerto = 0;
        ResultSet result = null;

        result = this.select.executeQuery("select count(*)*100/ (select count(*) from imagem_sub_fase b where b.ID_INSTANCIA_SUB_FASE = " + idInstanciaSubFase + ") as total\n"
                + "from imagem_sub_fase a\n"
                + "where a.ID_INSTANCIA_SUB_FASE = " + idInstanciaSubFase + "\n"
                + "and a.ACERTOU_RESPOSTA = 1");

        while (result.next()) {
            porcentagemAcerto = result.getLong("TOTAL");
        }

        return porcentagemAcerto;
    }

    @Override
    public void atualizaTempoSubFase(long idInstanciaSubFase, long tempoSubFase) throws Exception {

        CallableStatement cs;
        cs = this.con.getCon().prepareCall("call ATUALIZA_TEMPO_INSTANCIA_SUB_FASE(?,?)");

        cs.setLong(1, idInstanciaSubFase);
        cs.setLong(2, tempoSubFase);
        cs.execute();

        this.con.getStm().executeBatch();
    }

    @Override
    public long buscaSubFaseUltima(long idFase)  throws Exception  {
        long ultimaSubFase = 0;
        ResultSet result = null;

        result = this.select.executeQuery("select max(id_sub_fase) as ULTIMA_SUB_FASE from sub_fase where id_fase = "+idFase);

        while (result.next()) {
            ultimaSubFase = result.getLong("ULTIMA_SUB_FASE");
        }

        return ultimaSubFase;
    }
}
